use sqlparser::ast::*;
use sqlparser::dialect::MySqlDialect;
use sqlparser::parser::Parser;
use std::error::Error;
use sqlparser::ast::JsonTableColumnErrorHandling::Default;
use sqlparser::ast::TriggerObject::Statement;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let dialect = MySqlDialect {}; // or AnsiDialect

    let sql = "SELECT a, b, 123, myfunc(b) FROM table_1 WHERE a > b AND b < 100 ORDER BY a DESC, b";

    let parse_result = Parser::parse_sql(&dialect, sql);

    // println!("AST: {:?}", ast);

    // 修改 SELECT 子句为 COUNT(*)
    match parse_result {
        Ok(statements) => {
            let a = statements[0].clone();

            /*println!(
                "Round-trip:\n'{}'",
                statements
                    .iter()
                    .map(std::string::ToString::to_string)
                    .collect::<Vec<_>>()
                    .join("\n")
            );*/

            if cfg!(feature = "json_example") {
                #[cfg(feature = "json_example")]
                {
                    let serialized = serde_json::to_string_pretty(&statements).unwrap();
                    println!("Serialized as JSON:\n{serialized}");
                }
            } else {
                println!("Parse results:\n{statements:#?}");
            }

            std::process::exit(0);
        }
        Err(e) => {
            println!("Error during parsing: {e:?}");
            std::process::exit(1);
        }
    }


    Ok(())

}